import os
import pylab as plt
from params import *


YMIN = -6.5*1e6
YMAX = -3.8*1e6

deg_mle = os.path.join(WORKDIR, 'gowalla', 'temporal', "results","deg_mle_stats.txt")

x,y = [],[]
values = []
for line in open(deg_mle):
    alpha,mle = map(float,line.strip().split())
    x.append(alpha)
    y.append(mle)
    values.append((mle,alpha))


values.sort()
print 'DEGREE MLE alpha = ', values[-1][1]
def plot_mle(x,y,title,xmin=None,xmax=None):
    return
    #alternative size for more space on the left side
    MYFIG_AXES = [0.18,0.15,0.95-0.18,0.95-0.25]
    plt.figure()
    plt.clf()
    plt.axes(MYFIG_AXES)
    plt.plot(x,y,'ks-')#,mfc='none')
    plt.xlabel(r"$\alpha$")
    plt.ylabel("Log-likelihood")
    ax = plt.gca() 
    ax.ticklabel_format(style='sci', axis='y')
    ax.yaxis.major.formatter.set_powerlimits((0,0)) 
    if xmin:
        plt.xlim(xmin=xmin)
    if xmax:
        plt.xlim(xmax=xmax)
    plt.ylim((YMIN,YMAX))
    plt.grid(True)
    plt.savefig(title)
    plt.close()

plot_mle(x,y,'degree_mle.pdf',xmin = 0.0, xmax = 2.0)
degree_x = x
degree_y = y

age_mle = os.path.join(WORKDIR, 'gowalla', 'temporal', "results","age_mle_stats.txt")

x,y = [],[]
values = []
for line in open(age_mle):
    alpha,mle = map(float,line.strip().split())
    values.append((mle,alpha))

values.sort()
print 'AGE MLE alpha = ', values[-1][1]
values = [(b,a) for a,b in values]
values.sort()
x = [a for a,b in values]
y = [b for a,b in values]
age_x = x
age_y = y

plot_mle(x,y,'age_mle.pdf',xmin = -1.0, xmax = 1.0)

distance_mle = os.path.join(WORKDIR, 'gowalla', 'temporal', "results","distance_mle_stats.txt")

x,y = [],[]
values = []
for line in open(distance_mle):
    alpha,mle = map(float,line.strip().split())
    values.append((mle,alpha))

values.sort()
print 'DISTANCE MLE alpha = ', values[-1][1]
values = [(b,a) for a,b in values]
values.sort()
x = [a for a,b in values]
y = [b for a,b in values]
dist_x = x
dist_y = y

plot_mle(x,y,'distance_mle.pdf',xmin = 0.0, xmax = 2.0)

gravity_mle = os.path.join(WORKDIR, 'gowalla', 'temporal', "results","gravity_mle_stats.txt")
x,y = [],[]
values = []
for line in open(gravity_mle):
    alpha1, alpha2,mle = map(float,line.strip().split())
    values.append((mle,alpha2))

values.sort()
print 'GRAVITY MLE alpha = ', values[-1][1]
values = [(b,a) for a,b in values]
values.sort()
x = [a for a,b in values]
y = [b for a,b in values]
grav_x = x
grav_y = y

plot_mle(x,y,'gravity_mle.pdf',xmin = 0.0, xmax = 2.0)

MYFIG_AXES = [0.18,0.15,0.95-0.18,0.95-0.25]
plt.figure()
plt.clf()
plt.axes(MYFIG_AXES)
plt.plot(degree_x, degree_y, 'ks-',mfc='none', ms=6)
plt.plot(age_x, age_y, 'kd-',mfc='none', ms=6)
plt.plot(dist_x, dist_y, 'kv-',mfc='none', ms=6)
plt.plot(grav_x, grav_y, 'ko-',mfc='none', ms=6)
plt.legend([r'\textsf{%s}'%i for i in ('D', 'A', 'G', 'DG')],
    loc='lower left', numpoints=1)
plt.xlabel(r"$\alpha$")
plt.ylabel("Log-likelihood")
ax = plt.gca() 
ax.ticklabel_format(style='sci', axis='y')
ax.yaxis.major.formatter.set_powerlimits((0,0)) 
plt.xlim(xmin=0)
plt.xlim(xmax=2)
plt.ylim((YMIN,YMAX))
plt.grid(True)
plt.savefig('total_mle.pdf')
plt.close()
